package com.gdw.component;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;


public class BeanLifecycleComponent implements BeanNameAware {


    private UserBeans userBeans;

    public BeanLifecycleComponent(){
        System.out.println("执行了BeanLifecycleComponent构造方法");
    }

    @Autowired
    public void setUserComponent(UserBeans userBeans) {
        System.out.println("设置属性userComponent");
        this.userBeans = userBeans;
    }



    @Override
    public void setBeanName(String s) {
        System.out.println("执行通知 setBeanName 方法："+s);
    }

    /**
     * 初始化
     */
    @PostConstruct
    public void postConstruct() {
        System.out.println("执⾏ PostConstruct()");
    }

    public void init() {
        System.out.println("执⾏ BeanLifeComponent init-method");
    }

    public void use() {
        System.out.println("=== 使用bean ===");
        System.out.println(userBeans.getUser().getName());
        System.out.println("=== 使用bean ===");
    }

    /**
     * 销毁前执⾏⽅法
     */
    @PreDestroy
    public void preDestroy() {
        System.out.println("销毁前，执⾏：preDestroy()");
    }
}
